package com.kg.module.jtnc.util;

import cn.hutool.core.bean.BeanUtil;
import com.kg.core.exception.BaseException;
import com.kg.core.retgister.dto.RegisterFormDTO;
import com.kg.core.retgister.service.RegisterService;
import com.kg.core.security.util.CurrentUserUtils;
import com.kg.core.zuser.entity.ZUser;
import com.kg.module.jtnc.zzjh.entity.KrZzjh;
import com.kg.module.jtnc.zzjh.service.KrZzjhService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Sunshuwei
 * @date 2024-07-14
 */
@Component
public class PublicUtil {

    @Resource
    private RegisterService registerServiceAAA;
    @Resource
    private KrZzjhService krZzjhServiceAAA;

    private static RegisterService registerService;
    private static KrZzjhService krZzjhService;


    @PostConstruct
    public void init() {
        registerService = registerServiceAAA;
        krZzjhService = krZzjhServiceAAA;
    }

    public static void registerUser(RegisterFormDTO regFormDTO) throws BaseException {
        registerService.registerUser(regFormDTO);
    }

    public static void updateUser(RegisterFormDTO regFormDTO) throws BaseException {
        registerService.updateUser(regFormDTO);
    }

    public static void deleteUser(List<String> userIds) {
        registerService.deleteUser(userIds);
    }


    public static ZUser getUserInfo() {
        return CurrentUserUtils.getCurrentUser();
    }

    public static String getUserId() {
        return getUserInfo().getUserId();
    }
    public static String getUserNickName() {
        return getUserInfo().getNickName();
    }

    public static String getOrgId() {
        return getUserInfo().getOrgId();
    }

    public static List<String> getZzjhIdsByJsy() {
        List<KrZzjh> list = krZzjhService.lambdaQuery()
                .select(KrZzjh::getZzjhId)
                .eq(KrZzjh::getJsyId, getUserId())
                .eq(KrZzjh::getState, 1)
                .list();
        if(list.size()>0){
            return list.stream().map(KrZzjh::getZzjhId).collect(Collectors.toList());
        }
        return Collections.singletonList("-v-");
    }
    public static List<String> getZzjhIdsByNh()  {
        List<KrZzjh> list = krZzjhService.lambdaQuery()
                .select(KrZzjh::getZzjhId)
                .eq(KrZzjh::getNhId, getUserId())
                .eq(KrZzjh::getState, 1)
                .list();
        if(list.size()>0){
            return list.stream().map(KrZzjh::getZzjhId).collect(Collectors.toList());
        }
        return Collections.singletonList("-v-");
    }


    public static String getZzjhIdByDkId(String dkId) throws BaseException {
        KrZzjh one = krZzjhService.lambdaQuery()
                .select(KrZzjh::getZzjhId)
                .eq(KrZzjh::getDkglId, dkId)
                .eq(KrZzjh::getState, 1)
                .last("limit 1")
                .one();
        if(BeanUtil.isEmpty(one)){
            throw new BaseException("该地块未查询到正在启用的种植计划");
        }
        return one.getZzjhId();
    }
    public static KrZzjh getZzjhEntityByDkId(String dkId) throws BaseException {
        KrZzjh one = krZzjhService.lambdaQuery()
                .select(KrZzjh::getZzjhId)
                .eq(KrZzjh::getDkglId, dkId)
                .eq(KrZzjh::getState, 1)
                .last("limit 1")
                .one();
        if(BeanUtil.isEmpty(one)){
            throw new BaseException("该地块未查询到正在启用的种植计划");
        }
        return one;
    }


}
